<template>
  <div class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="车间编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入" clearable />
        </el-form-item>

        <el-form-item label="车间名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" type="textarea" placeholder="请输入" clearable />
        </el-form-item>

        <el-form-item label="工厂平面图">
          <FileManageFull
            :file-names="formData.fileName"
            :file-real-names="formData.fileRealName"
            :file-urls="formData.fileUrl"
            :row-index="1"
            single-multiple="single"
            @fileName="setFileName"
          />
        </el-form-item>

      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdate, getInfo } from '@/api/equipmentManage/andonManage/andonManage'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    defaultValue: {},
    id: {}
  },
  data() {
    return {
      formData: {
        id: '',
        code: '',
        name: '',
        fileName: '',
        fileRealName: '',
        fileUrl: '',
        remark: ''
      },
      rules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'change' }
        ],
        name: [
          { required: true, message: '名称不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (!this.id) { // 没有data就是新增
    } else { // 有data就是编辑
      this.getById()
    }
  },
  methods: {
    getById() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
      })
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.getList() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },

    setFileName(...data) {
      this.formData.fileName = data[0]
      this.formData.fileRealName = data[1]
      this.formData.fileUrl = data[2]
    }
  }
}
</script>

<style lang="less" scoped>

</style>
